Page({

  /**
   * 页面的初始数据
   */
  data: {
    inputText: '',
    result: '',
    latitude: '',
    longitude: '',
    pictures: [
      [],
      []
    ],
    gymInfo: [{
      gymName: 'XX健身馆',
      picture: [],
      location: '河南郑州',
      phone: '18338350145',
      distance: '10.5742',
      id: '',
      state:1
    }, {
      gymName: 'XX健身馆',
      picture: '',
      location: '河南郑州',
      phone: '18338350145',
      distance: '10.5742',
      gymid: '',
      state:1
    }, ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.getLocation({
      type: 'wgs84',
      success: res => {
        console.log(res)
        this.setData({
          latitude: res.latitude,
          longitude: res.longitude,
        })
        this.getList();
      },
      fail: function (errInfo) {
        console.info(errInfo)
      }
    })
    console.log(this.data.gymInfo)
  },
// 获取整个列表
  getList: function() {
    wx.request({
      url: 'http://localhost:8080/keep/gym/' + this.data.longitude + '/' + this.data.latitude + '/' + 2,
      data: {},
      method: "POST",
      header: {
        'Authorization': getApp().globalData.authorization
      },
      success: (resp) => {
        console.log("成功")
        console.log(resp)
        const items = resp.data;
        for (let i = 0; i < items.length; i++) {
          console.log("======================================")
          this.data.pictures[i] = items[i].picture.split(',')
          items[i].picture = this.data.pictures[i][0]
          // 在这里执行你需要的操作
        }
        this.setData({
          gymInfo: resp.data
        })
      },
      fail: resp => {
        console.log("失败")
      },
    })
  },
  // 防抖函数
  debounce: function(func, wait) {
    let timeout;
    return function() {
      const context = this;
      const args = arguments;
      clearTimeout(timeout);
      timeout = setTimeout(() => func.apply(context, args), wait);
    };
  },
// 输入东西触发事件
  onInputChange: function(e) {
    const inputValue = e.detail.value;
    this.setData({
      inputText: inputValue
    });

    // 使用防抖函数来限制请求频率
    this.debouncedPerformRequest(inputValue);
  },
// 判断输入清除后为空执行什么
  performRequest: function(input) {
    if (input.trim() === '') {
      this.performRequestA();
    } else {
      this.performRequestB(input);
    }
  },


  performRequestA: function() {
    this.getList();
  },
  performRequestB: function(input) {
   const inputText= this.data.inputText
    // 如果输入搜索时获取的列表
    wx.request({
      url: 'http://localhost:8080/keep/gym/gymid/name' ,
      data: {
        gymName:inputText
      },
      method: "GET",
      header: {
        'Authorization': getApp().globalData.authorization
      },
      success: (resp) => {
        console.log("成功")
        console.log(resp)
        const items = resp.data;
        for (let i = 0; i < items.length; i++) {
          console.log("======================================")
          this.data.pictures[i] = items[i].picture.split(',')
          items[i].picture = this.data.pictures[i][0]
          // 在这里执行你需要的操作
        }
        this.setData({
          gymInfo: resp.data
        })
      },
      fail: resp => {
        console.log("失败")
      },
    })
  },
   // 使用防抖函数包装 performRequest
   debouncedPerformRequest: function (input) {
     this.debounce(this.performRequest(input), 300).bind(this)
   },
// 详情页面跳转
  godetail() {
    wx.navigateTo({
      url: '/pages/venue/venuedetil/venuedetill',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})